package com.wall.springbootinit.controller;

import com.wall.springbootinit.common.BaseResponse;
import com.wall.springbootinit.common.ErrorCode;
import com.wall.springbootinit.common.ResultUtils;
import com.wall.springbootinit.exception.BusinessException;
import com.wall.springbootinit.model.dto.menu.MenuDto;
import com.wall.springbootinit.model.dto.menu.MenuUpdateDto;
import com.wall.springbootinit.model.entity.File;
import com.wall.springbootinit.model.entity.Menu;
import com.wall.springbootinit.model.vo.MenuTreeVo;
import com.wall.springbootinit.service.MenuService;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author lzw
 */
@RestController
@RequestMapping("/menu")
public class MenuController {

    @Resource
    private MenuService menuService;

    /**
     * 添加菜单
     * @param menu 菜单
     * @return 结果
     */
    @PostMapping("/add")
    public BaseResponse<Boolean> addMenu(@RequestBody MenuDto menu) {
        if (menu == null){
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }
        boolean b = menuService.addMenu(menu);
        return ResultUtils.success(b);
    }

    /**
     * 删除菜单
     * @param businessId 菜单id
     * @return 结果
     */
    @DeleteMapping("/delete")
    public BaseResponse<Boolean> deleteMenu(@RequestParam String businessId) {
        if (businessId == null){
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }
        boolean b = menuService.removeById(businessId);
        return ResultUtils.success(b);
    }

    /**
     * 更新菜单
     * @param menu 菜单
     * @return 结果
     */
    @PutMapping("/update")
    public BaseResponse<Boolean> updateMenu(@RequestBody MenuUpdateDto menu) {
        if (menu == null){
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }
        boolean b = menuService.updateMenu(menu);
        return ResultUtils.success(b);
    }

    /**
     * 获取菜单
     * @param id 菜单
     * @return 结果
     */
    @GetMapping("/get/{id}")
    public BaseResponse<List<File>> getMenu(@PathVariable Long id) {
        if (id == null){
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }
        List<File> fileList = menuService.getMenuImageList(id);
        return ResultUtils.success(fileList);
    }

    /**
     * 获取菜单列表
     * @return 结果
     */
    @PostMapping("/list")
    public BaseResponse<List<Menu>> listMenu() {
        List<Menu> menu1 = menuService.list();
        return ResultUtils.success(menu1);
    }


    /**
     * 获取菜单树
     * @return 结果
     */
    @PostMapping("/tree")
    public BaseResponse<MenuTreeVo> treeMenu() {
        MenuTreeVo menuTreeVo = menuService.getTree();
        return ResultUtils.success(menuTreeVo);
    }
}
